<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: Like Old Dream
  Date: 2020/12/22
  Time: 10:38
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/statics/css/login.css"/>
<head>
    <title>登录</title>
</head>
<body>
<div id="login">
    <h1>Login</h1>
    <input type="hidden" id="path" value="${pageContext.request.contextPath}">
        <label>用户名:</label><input type="text" name="username" class="log" id="username"/>
    <label id="unameMsg" style="color: red"></label><br>
        <label>密码:</label><input type="password" name="password" class="log" id="password"/>
    <label id="upwdMsg" style="color: red"></label><br>
        <div class="item-input" style=" display: flex;">
            <input id="j_captcha" style="width:70px;" name="j_captcha" type="text" class="log" placeholder="验证码" />
            <img id="captcha_img" alt="点击更换" width="60" height="21"  title="点击更换" onclick="changeVerifyCode(this)" src="<c:url value="/out/getVerifyCode"/>" />
            <label id="check" style="color: red"></label><br>
        </div>
    <input value="登录" class="but"  type="button" onclick="requestJson()"/>
</div>
<script type="text/javascript" src="${pageContext.request.contextPath}/statics/js/jquery-3.5.1.js"></script>
    <script type="text/javascript">
        function requestJson() {
            var path = $("#path").val();
            var username = $("#username").val();
            var password = $("#password").val();
            var j_captcha = $("#j_captcha").val();
            if (username == null || username === "") {
                $("#unameMsg").html("用户名不能为空!");
                return false;
            } else if (password === "" || password == null) {
                $("#unameMsg").html("");
                $("#upwdMsg").html("密码不能为空!");
                return false;
            } else if (password.length < 6 || password.length > 12) {
               /* $("#upwdMsg").html("");*/
                $("#upwdMsg").html("密码应该在 6-12 位之间!");
                return false;
            } else if (j_captcha === "" || j_captcha == null) {
                $("#upwdMsg").html("");
                $("#check").html("请输入验证码");
                return false;
            }else {
                $("#check").html("");
                $.getJSON(path+"/out/jquey",{username:username,password:password,j_captcha:j_captcha},function (data) {
                    if (data.username=="yes"){
                       alert("登录成功")
                       window.location.href="${pageContext.request.contextPath}/user/omg"
                    ;}else if(data.username=="sos"){
                        alert("管理员登录成功")
                        window.location.href="${pageContext.request.contextPath}/user/omg"
                    }else if(data.username=="no"){
                      alert("信息错误,请核实信息")
                    }
                })
            }

        }
        function changeVerifyCode(img) {
            img.src = "${pageContext.request.contextPath}/out/getVerifyCode?"+ Math.floor(Math.random() * 100);
        }
    </script>
</body>
</html>
